package com.zzmcc.jms.controller;

import com.jfinal.aop.Before;
import com.jfinal.ext.render.excel.JxlsRender;
import com.jfinal.kit.PathKit;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.zzmcc.base.BaseController;
import com.zzmcc.common.AuthorizeInterceptor;
import com.zzmcc.jms.model.ProjectBuild;
import util.ChristStringUtil;
import util.ModelUtils;

import java.text.SimpleDateFormat;
import java.util.*;

import static com.zzmcc.jms.common.ModelConfigWms.TABLE_Project_Build;

public class ProjectCheckController extends BaseController {
    private static final Logger log = Logger.getLogger(ProjectCheckController.class);

    @Before(AuthorizeInterceptor.class)
    public void index() {
        StringBuffer whee = new StringBuffer();
        List<Object> param = new ArrayList<Object>();

        String name = getPara("project.name");
        String code = getPara("project.code");
        String projectOwner = getPara("project.projectOwner");
        String homeCounty = getPara("project.homeCounty");
        String isProject = getPara("project.isProject");
        String isCm = getPara("project.isCm");
        String isCu = getPara("project.isCu");
        String isCt = getPara("project.isCt");
        String designCompare = getPara("project.designCompare");
        String startCompare = getPara("project.startCompare");
        String completeCompare = getPara("project.completeCompare");
        String isSubmit = getPara("project.isSubmit");
        String modifyDate = getPara("project.modifyDate");

        if (ChristStringUtil.isNotEmpty(name)) {
            whee.append(" and name like ?");
            param.add("%" + name + "%");
        }
        if (ChristStringUtil.isNotEmpty(projectOwner)) {
            whee.append(" and projectOwner like ?");
            param.add("%" + projectOwner + "%");
        }
        if (ChristStringUtil.isNotEmpty(code)) {
            whee.append(" and a.code like ?");
            param.add("%" + code + "%");
        }
        if (ChristStringUtil.isNotEmpty(homeCounty)) {
            whee.append(" and homeCounty = ?");
            param.add(homeCounty);
        }
        if (ChristStringUtil.isNotEmpty(designCompare)) {
            if (designCompare.equals("相同")) {
                whee.append(" and b.isDesign = c.isDesign");
            } else {
                whee.append(" and b.isDesign <> c.isDesign");
            }

        }
        if (ChristStringUtil.isNotEmpty(startCompare)) {
            if (startCompare.equals("相同")) {
                whee.append(" and b.isStart = c.isStart");
            } else {
                whee.append(" and b.isStart <> c.isStart");
            }

        }
        if (ChristStringUtil.isNotEmpty(completeCompare)) {
            if (completeCompare.equals("相同")) {
                whee.append(" and b.isComplete = c.isComplete");
            } else {
                whee.append(" and b.isComplete <> c.isComplete");
            }

        }

        if (ChristStringUtil.isNotEmpty(isProject)) {
            whee.append(" and isProject = ?");
            param.add(isProject);
        }

        if (ChristStringUtil.isNotEmpty(isCm)) {
            whee.append(" and isCm = ?");
            param.add(isCm);
        }
        if (ChristStringUtil.isNotEmpty(isCt)) {
            whee.append(" and isCt = ?");
            param.add(isCt);
        }
        if (ChristStringUtil.isNotEmpty(isCu)) {
            whee.append(" and isCu = ?");
            param.add(isCu);
        }
        if (ChristStringUtil.isNotEmpty(modifyDate)) {
            whee.append(" and TO_DAYS(b.modifyDate) >= TO_DAYS(?)");
            param.add(modifyDate);
        }
        String selectStr = "select a.id,a.homeCounty,a.name,a.share,a.projectDate,a.projectOwner,a.isProject,isCm,isCt,isCu," +
                "b.isDesign,b.isStart,b.isComplete,b.modifyMemo," +
                "(case when b.isDesign=c.isDesign then '相同' else '不同' end ) designCompare," +
                "(case when b.isStart=c.isStart then '相同' else '不同' end ) startCompare," +
                "(case when b.isComplete=c.isComplete then '相同' else '不同' end ) completeCompare," +
                "c.isDesign isDesign2,c.isStart isStart2,c.isComplete isComplete2,c.modifyMemo modifyMemo2,c.isElect,c.isBase,c.isBody,c.isWireless";
        String fromStr = " from jms_project a left join jms_project_build b on b.pid=a.pid  " +
                " left join jms_project_inspect c on c.pid=a.pid  " +
                " where a.isEntrust='是' " + whee.toString() + "  order by b.modifyDate desc";
        Page<ProjectBuild> pager = ProjectBuild.dao.paginate(
                getParaToInt("pageNum", 1),
                getParaToInt("numPerPage", 20),
                selectStr, fromStr,
                param.toArray());

        setAttr("pager", pager);
        setAttr("name", name);
        setAttr("projectOwner", projectOwner);
        setAttr("code", code);
        setAttr("homeCounty", homeCounty);
        setAttr("isProject", isProject);
        setAttr("isCm", isCm);
        setAttr("isCu", isCu);
        setAttr("isCt", isCt);
        setAttr("designCompare", designCompare);
        setAttr("startCompare", startCompare);
        setAttr("completeCompare", completeCompare);

        setAttr("isSubmit", isSubmit);
        setAttr("isCm", isCm);
        setAttr("isCt", isCt);
        setAttr("isCu", isCu);
        setAttr("modifyDate", modifyDate);
        render("../project_check_list.html");
    }


    public void export() {

        //导出条件
        StringBuffer whee = new StringBuffer();
        List<Object> param = new ArrayList<Object>();


        String homeCounty = getPara("project.homeCounty");
        String projectOwner = getPara("project.projectOwner");
        String isCm = getPara("project.isCm");
        String isCu = getPara("project.isCu");
        String isCt = getPara("project.isCt");
        String modifyDate = getPara("project.modifyDate");


        if (ChristStringUtil.isNotEmpty(projectOwner)) {
            whee.append(" and projectOwner like ?");
            param.add("%" + projectOwner + "%");
        }
        if (ChristStringUtil.isNotEmpty(homeCounty)) {
            whee.append(" and homeCounty = ?");
            param.add(homeCounty);
        }

        if (ChristStringUtil.isNotEmpty(isCm)) {
            whee.append(" and isCm = ?");
            param.add(isCm);
        }
        if (ChristStringUtil.isNotEmpty(isCt)) {
            whee.append(" and isCt = ?");
            param.add(isCt);
        }
        if (ChristStringUtil.isNotEmpty(isCu)) {
            whee.append(" and isCu = ?");
            param.add(isCu);
        }
        if (ChristStringUtil.isNotEmpty(modifyDate)) {
            whee.append(" and TO_DAYS(modifyDate) >= TO_DAYS(?)");
            param.add(modifyDate);
        }


        String sql = "select a.homeCounty,a.name,a.code,a.longitude,a.latitude,a.addr,\n" +
                "a.isProject,a.projectDate,a.projectOwner,a.isCm,a.isCt,a.isCu, \n" +
                "b.isReach,b.isStart,b.isDesign,b.isComplete,b.isSubmit,b.modifyDate,b.modifyMemo,b.memo\n" +
                " from jms_project a\n" +
                "left join\n" +
                "jms_project_build b\n" +
                "on a.code=b.code\n" +
                "where 1=1 " + whee.toString() + "order by b.modifyDate desc";
        List<Record> buildDetail = Db.find(sql, param.toArray());


        Map<String, Object> beans = new HashMap<String, Object>();
        beans.put("buildDetail", buildDetail);


        String templateFileName = PathKit.getRootClassPath() + "/tpl_project_check.xls";

        String filename = "project_check.xls";
        render(JxlsRender.me(templateFileName).filename(filename).beans(beans));
    }


    @Before(AuthorizeInterceptor.class)
    public void delete() {

        String[] ids = getParaValues("ids");
        for (String id : ids) {
            ProjectBuild.dao.deleteById(id);
        }
        renderDWZSuccessJson("删除成功!");

    }

}
